<html>
<head><meta charset="utf-8"><title>Suggestions for a beginner · t-compiler/wg-mir-opt · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/index.html">t-compiler/wg-mir-opt</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html">Suggestions for a beginner</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="237560574"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions%20for%20a%20beginner/near/237560574" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html#237560574">(May 05 2021 at 20:33)</a>:</h4>
<p>Hello! I'm new to rustc and would like to contribute writing MIR optimizations. I've read most of the rustc book and a few existing opts, and now I am wondering if anybody would have a suggestion of something that needs work and would be accessible to a beginner like me. Thanks in advance!</p>



<a name="237710540"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions%20for%20a%20beginner/near/237710540" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html#237710540">(May 06 2021 at 18:53)</a>:</h4>
<p>This doesn't answer your question, but once you have an optimization to implement, you may want to look at <a href="https://rustc-dev-guide.rust-lang.org/mir/optimizations.html#quickstart-for-adding-a-new-optimization">this newly-added Quickstart section of the docs</a> if you haven't already :)</p>



<a name="237718099"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions%20for%20a%20beginner/near/237718099" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html#237718099">(May 06 2021 at 19:47)</a>:</h4>
<p>Thank you! I had indeed read it and it's actually what sparkled the interest for me to try it out ahah</p>



<a name="237718259"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions%20for%20a%20beginner/near/237718259" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html#237718259">(May 06 2021 at 19:48)</a>:</h4>
<p>I figured it's not a very easy question to answer to so what I think I'm going to do is to read some random MIR I'll generate from different crates to try and spot parts that would be inefficient.</p>



<a name="237721436"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions%20for%20a%20beginner/near/237721436" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html#237721436">(May 06 2021 at 20:14)</a>:</h4>
<p>I don't know if it'd be accessible to a beginner, but it'd be wonderful if someone could pick up the work to improve the <code>?</code> patterns in MIR.</p>
<p>There's &lt;<a href="https://github.com/rust-lang/rust/blob/master/src/test/mir-opt/simplify-arm.rs">https://github.com/rust-lang/rust/blob/master/src/test/mir-opt/simplify-arm.rs</a>&gt;, but that transform is disabled for unsoundness, and with <a href="https://github.com/rust-lang/RFCs/issues/3058">RFCs#3058</a> it isn't the pattern that <code>?</code> will produce anyway, as <a href="https://github.com/rust-lang/rust/pull/84767">the desugar</a> will go <code>Result</code>-&gt;<code>ControlFlow</code>-&gt;<code>Result</code>.</p>



<a name="237722064"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions%20for%20a%20beginner/near/237722064" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html#237722064">(May 06 2021 at 20:19)</a>:</h4>
<p>Thank you, I'll check it out asap.</p>



<a name="237722191"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions%20for%20a%20beginner/near/237722191" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html#237722191">(May 06 2021 at 20:20)</a>:</h4>
<p>Bad ascii art alert:</p>
<p>I think it's currently doing something like</p>
<div class="codehilite"><pre><span></span><code>               handle ok                      handle continue
             /           \                   /
match result               match controlflow
             \           /                   \
               handle err                      handle break
</code></pre></div>
<p>And it would be amazing to detect that the match is just switching over what we just set, and redirect the terminators to</p>
<div class="codehilite"><pre><span></span><code>               handle ok  -- handle continue
             /
match result
             \
               handle err -- handle break
</code></pre></div>



<a name="237723045"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions%20for%20a%20beginner/near/237723045" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html#237723045">(May 06 2021 at 20:27)</a>:</h4>
<p>Okay, I think I understand. I'll look at an MIR dump of that using your PR to see exactly what needs to be done and I'll try to give it a go.</p>



<a name="237723182"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions%20for%20a%20beginner/near/237723182" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html#237723182">(May 06 2021 at 20:28)</a>:</h4>
<p>Quick godbolt of the structur: <a href="https://rust.godbolt.org/z/o6nxszWKM">https://rust.godbolt.org/z/o6nxszWKM</a></p>
<p>(The traits are in nightly already, so ideally you wouldn't have to couple yourself to my currently-regresses-perf PR)</p>



<a name="237723262"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions%20for%20a%20beginner/near/237723262" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html#237723262">(May 06 2021 at 20:29)</a>:</h4>
<p>Awesome! I did not know godbolt could show MIR. This tool is great.</p>



<a name="237723424"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions%20for%20a%20beginner/near/237723424" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html#237723424">(May 06 2021 at 20:30)</a>:</h4>
<p>And FWIW, I'm not an "official" compiler dev, so it's possible this is a bad idea.  Don't put weeks of work into it without confirming with people who know better than I whether it's worth doing <span aria-label="upside down" class="emoji emoji-1f643" role="img" title="upside down">:upside_down:</span></p>



<a name="237723657"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions%20for%20a%20beginner/near/237723657" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html#237723657">(May 06 2021 at 20:32)</a>:</h4>
<p>Ahah okay. Don't worry though, I'm doing that for fun and practice, it's okay if it ends up unused.</p>



<a name="237723727"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions%20for%20a%20beginner/near/237723727" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html#237723727">(May 06 2021 at 20:32)</a>:</h4>
<p>Another related idea I had: do some simple tail block simplification.</p>
<p>For example, if you have <code>?</code> 5 times in a function, there will be 5 blocks (well, 10 I guess, since the return is in a different block) generated for <code>return FromResidual::from_residual(r)</code>.  I suspect that the type of that residual will often be the same -- perhaps it's always on an <code>io::Result</code>, say -- so it would be possible to just remove the duplicate blocks and have all the <code>?</code> CFG edges go to the same block.</p>



<a name="237724150"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions%20for%20a%20beginner/near/237724150" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html#237724150">(May 06 2021 at 20:35)</a>:</h4>
<p>wouldn't that be picked up by the identical blocks optimization pass?</p>



<a name="237724392"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions%20for%20a%20beginner/near/237724392" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html#237724392">(May 06 2021 at 20:37)</a>:</h4>
<p>Maybe?  I have no idea.  (Would probably depend if the identical blocks pass applies even if the local variables are different -- what's the word, alpha-normalization?)</p>



<a name="237724619"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions%20for%20a%20beginner/near/237724619" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html#237724619">(May 06 2021 at 20:38)</a>:</h4>
<p>I think it does not (alpha-normalization sounds right)<br>
Okay then</p>



<a name="237724621"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions%20for%20a%20beginner/near/237724621" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html#237724621">(May 06 2021 at 20:38)</a>:</h4>
<p>It looks like, no, <span class="user-mention" data-user-id="409735">@Théo Degioanni</span>, based on <a href="https://rust.godbolt.org/z/sj6rYTorj">https://rust.godbolt.org/z/sj6rYTorj</a></p>



<a name="237724654"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions%20for%20a%20beginner/near/237724654" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html#237724654">(May 06 2021 at 20:39)</a>:</h4>
<p>That is in line with what I recall from that pass</p>



<a name="237724696"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions%20for%20a%20beginner/near/237724696" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html#237724696">(May 06 2021 at 20:39)</a>:</h4>
<p>I think I'll have to document myself a bit on on try v2 first though</p>



<a name="237725051"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions%20for%20a%20beginner/near/237725051" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html#237725051">(May 06 2021 at 20:42)</a>:</h4>
<p>Feel free to ping me if there's anything I can help answer, or if it'd be useful for me to split out the lang items from that massive PR or something.</p>



<a name="237729426"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions%20for%20a%20beginner/near/237729426" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html#237729426">(May 06 2021 at 21:06)</a>:</h4>
<p>And a more complete godbolt, with the old and new desugarings for an identity <code>?</code> showing that it's not an identity in ASM (for either of them): <a href="https://rust.godbolt.org/z/qsY8Mq311">https://rust.godbolt.org/z/qsY8Mq311</a></p>



<a name="237734921"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions%20for%20a%20beginner/near/237734921" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html#237734921">(May 06 2021 at 21:39)</a>:</h4>
<p><span class="user-mention" data-user-id="125270">@scottmcm</span> So I think I got confused thinking about those examples. The point of the optimization would be that when an <code>Err</code> variant gets deconstructed into a <code>ControlFlow</code> then back into an <code>Err</code> variant of the same type, one could just turn that into the identity, is that it?</p>



<a name="237738783"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions%20for%20a%20beginner/near/237738783" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html#237738783">(May 06 2021 at 22:13)</a>:</h4>
<p>That's probably the most common case, yeah -- like <code>?</code> on a <code>std::fmt::Result</code> in a method that returns <code>std::fmt::Result</code>.</p>



<a name="237739060"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions%20for%20a%20beginner/near/237739060" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html#237739060">(May 06 2021 at 22:15)</a>:</h4>
<p>I think the difficult part is to assert that this operation is indeed an identity</p>



<a name="237739078"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions%20for%20a%20beginner/near/237739078" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html#237739078">(May 06 2021 at 22:16)</a>:</h4>
<p>I don't think there's any semantic guarantee that it is right now</p>



<a name="237739159"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions%20for%20a%20beginner/near/237739159" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html#237739159">(May 06 2021 at 22:16)</a>:</h4>
<p>I think the existing ones were made to run after the mir inliner, where one can see the variants directly and thus actually know it.</p>



<a name="237739191"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions%20for%20a%20beginner/near/237739191" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html#237739191">(May 06 2021 at 22:16)</a>:</h4>
<p>aah that sounds more reasonable</p>



<a name="237739223"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions%20for%20a%20beginner/near/237739223" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html#237739223">(May 06 2021 at 22:17)</a>:</h4>
<p>Although without inlining I don't expect it to be reasonably possible I'm afraid :/</p>



<a name="237739360"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions%20for%20a%20beginner/near/237739360" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html#237739360">(May 06 2021 at 22:18)</a>:</h4>
<p>Another shot at trying to describe what I was trying to do in the ascii art:</p>
<p>Transforming</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">    </span><span class="k">match</span><span class="w"> </span><span class="p">(</span><span class="k">match</span><span class="w"> </span><span class="n">some_result</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nb">Ok</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">ControlFlow</span>::<span class="n">Continue</span><span class="p">(</span><span class="n">x</span><span class="p">),</span><span class="w"></span>
<span class="w">        </span><span class="nb">Err</span><span class="p">(</span><span class="n">y</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">ControlFlow</span>::<span class="n">Break</span><span class="p">(</span><span class="n">y</span><span class="p">),</span><span class="w"></span>
<span class="w">    </span><span class="p">})</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">ControlFlow</span>::<span class="n">Continue</span><span class="p">(</span><span class="n">a</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">a</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="n">ControlFlow</span>::<span class="n">Break</span><span class="p">(</span><span class="n">b</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">do_something_with</span><span class="p">(</span><span class="n">b</span><span class="p">),</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>into</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">    </span><span class="k">match</span><span class="w"> </span><span class="n">some_result</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nb">Ok</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">x</span><span class="p">;</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="p">},</span><span class="w"></span>
<span class="w">        </span><span class="nb">Err</span><span class="p">(</span><span class="n">y</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">y</span><span class="p">;</span><span class="w"> </span><span class="n">do_something_with</span><span class="p">(</span><span class="n">b</span><span class="p">)</span><span class="w"> </span><span class="p">},</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>in general.</p>



<a name="237739475"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions%20for%20a%20beginner/near/237739475" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html#237739475">(May 06 2021 at 22:20)</a>:</h4>
<p>Kinda like inlining the outer <code>match</code> into the inner arm, since the inner arm is always returning the same discriminant so will always go the same direction in the later match.</p>



<a name="237739525"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions%20for%20a%20beginner/near/237739525" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html#237739525">(May 06 2021 at 22:20)</a>:</h4>
<p>(as that one would get picked up for even non-identity cases)</p>



<a name="237739672"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions%20for%20a%20beginner/near/237739672" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html#237739672">(May 06 2021 at 22:21)</a>:</h4>
<p>That would be relatively easy in SSA, AFAICT, but might be a pain in MIR...</p>



<a name="237745017"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions%20for%20a%20beginner/near/237745017" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html#237745017">(May 06 2021 at 23:17)</a>:</h4>
<p><span class="user-mention silent" data-user-id="125270">scottmcm</span> <a href="#narrow/stream/189540-t-compiler.2Fwg-mir-opt/topic/Suggestions.20for.20a.20beginner/near/237739360">said</a>:</p>
<blockquote>
<p>Another shot at trying to describe what I was trying to do in the ascii art:</p>
<p>Transforming</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">    </span><span class="k">match</span><span class="w"> </span><span class="p">(</span><span class="k">match</span><span class="w"> </span><span class="n">some_result</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nb">Ok</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">ControlFlow</span>::<span class="n">Continue</span><span class="p">(</span><span class="n">x</span><span class="p">),</span><span class="w"></span>
<span class="w">        </span><span class="nb">Err</span><span class="p">(</span><span class="n">y</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">ControlFlow</span>::<span class="n">Break</span><span class="p">(</span><span class="n">y</span><span class="p">),</span><span class="w"></span>
<span class="w">    </span><span class="p">})</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">ControlFlow</span>::<span class="n">Continue</span><span class="p">(</span><span class="n">a</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">a</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="n">ControlFlow</span>::<span class="n">Break</span><span class="p">(</span><span class="n">b</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">do_something_with</span><span class="p">(</span><span class="n">b</span><span class="p">),</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>into</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">    </span><span class="k">match</span><span class="w"> </span><span class="n">some_result</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nb">Ok</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">x</span><span class="p">;</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="p">},</span><span class="w"></span>
<span class="w">        </span><span class="nb">Err</span><span class="p">(</span><span class="n">y</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">y</span><span class="p">;</span><span class="w"> </span><span class="n">do_something_with</span><span class="p">(</span><span class="n">b</span><span class="p">)</span><span class="w"> </span><span class="p">},</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>in general.</p>
</blockquote>
<p>I think this might be what's known as a "case-of-case" optimization. This Stack Overflow answer is the only explanation I've found of this sort of optimization so far: <a href="https://stackoverflow.com/a/35815694">https://stackoverflow.com/a/35815694</a></p>



<a name="237745110"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions%20for%20a%20beginner/near/237745110" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html#237745110">(May 06 2021 at 23:18)</a>:</h4>
<p>Just guessing, but this kind of optimization might be kind of hard to do in the MIR, since matches are fully desugared. Maybe it could be done "on the fly" as part of MIR building?</p>



<a name="237745146"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions%20for%20a%20beginner/near/237745146" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html#237745146">(May 06 2021 at 23:19)</a>:</h4>
<p>It would probably be helpful if Rust had a higher-level IR that was simpler than HIR.</p>



<a name="237745545"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions%20for%20a%20beginner/near/237745545" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html#237745545">(May 06 2021 at 23:24)</a>:</h4>
<p>I think in practice this is also going to be hard because you need to inspect the inner implementation as well, which may or may not be trivial. I'll give it some thought tomorrow.</p>



<a name="237746053"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions%20for%20a%20beginner/near/237746053" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html#237746053">(May 06 2021 at 23:30)</a>:</h4>
<p><span class="user-mention silent" data-user-id="307537">Camelid</span> <a href="#narrow/stream/189540-t-compiler.2Fwg-mir-opt/topic/Suggestions.20for.20a.20beginner/near/237745110">said</a>:</p>
<blockquote>
<p>Just guessing, but this kind of optimization might be kind of hard to do in the MIR, since matches are fully desugared. Maybe it could be done "on the fly" as part of MIR building?</p>
</blockquote>
<p>Actually, IIUC, <code>?</code> lowering happens during AST -&gt; HIR, <em>not</em> THIR -&gt; MIR. So this maybe would happen on the fly during AST lowering?</p>



<a name="237749176"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions%20for%20a%20beginner/near/237749176" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html#237749176">(May 07 2021 at 00:08)</a>:</h4>
<p>Well, it can't happen in AST lowering, because that doesn't have inlining to see both matches.  (The first is inside the call to <code>Try::branch</code>, and we haven't even run type checking yet, so <em>definitely</em> can't see that <code>match</code>.)</p>



<a name="237749831"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions%20for%20a%20beginner/near/237749831" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html#237749831">(May 07 2021 at 00:17)</a>:</h4>
<p><span class="user-mention silent" data-user-id="307537">Camelid</span> <a href="#narrow/stream/189540-t-compiler.2Fwg-mir-opt/topic/Suggestions.20for.20a.20beginner/near/237745110">said</a>:</p>
<blockquote>
<p>Just guessing, but this kind of optimization might be kind of hard to do in the MIR, since matches are fully desugared. Maybe it could be done "on the fly" as part of MIR building?</p>
</blockquote>
<p>I think the desugared-ness will actually make it easier.  Basically, you're looking for <code>discriminant(_2) = 1;</code>in a block that branches to a block that does <code>_4 = discriminant(_2); switchInt(move _4) -&gt; ...</code>.</p>
<p>(I guess any place would work, not just discriminant, really.  I wonder how close the const prop optimization is to already doing this...)</p>



<a name="237750034"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions%20for%20a%20beginner/near/237750034" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html#237750034">(May 07 2021 at 00:20)</a>:</h4>
<p>Hmm, interesting -- with <code>-Z mir-opt-level=3</code> it'll inline <code>TryV2::from_output</code> and <code>TryV2::from_residual</code>, but not <code>TryV2::branch</code>: <a href="https://rust.godbolt.org/z/KTjE9hhxr">https://rust.godbolt.org/z/KTjE9hhxr</a></p>
<p>(And yes, <code>branch</code> is <code>#[inline]</code>: <a href="https://github.com/rust-lang/rust/blob/676ee14729462585b969bbc52f32c307403f4126/library/core/src/result.rs#L1660-L1666">https://github.com/rust-lang/rust/blob/676ee14729462585b969bbc52f32c307403f4126/library/core/src/result.rs#L1660-L1666</a> )</p>



<a name="237834564"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions%20for%20a%20beginner/near/237834564" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html#237834564">(May 07 2021 at 14:46)</a>:</h4>
<p>Oh. All this time I was not aware inlining could already happen before MIR optimizations are ran. Welp, sorry about that ^^'</p>



<a name="237834730"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions%20for%20a%20beginner/near/237834730" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html#237834730">(May 07 2021 at 14:47)</a>:</h4>
<p>If branch can be inlined, I think I get how to implement that. Find a match-looking switch, walk backwards in the cfg to find another match-looking switch, and try to merge them</p>



<a name="237834893"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions%20for%20a%20beginner/near/237834893" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html#237834893">(May 07 2021 at 14:48)</a>:</h4>
<p>I was trying to wrap my head around the drawbacks of maybe generating a custom version of branch that does some weird stuff to circumvent the lack of inlining, but if inlining is there it's not as hard.</p>



<a name="237871133"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions%20for%20a%20beginner/near/237871133" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html#237871133">(May 07 2021 at 18:42)</a>:</h4>
<p>Ah, yes, that would be important <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span> </p>
<p>The inliner is &lt;<a href="https://github.com/rust-lang/rust/blob/master/compiler/rustc_mir/src/transform/inline.rs">https://github.com/rust-lang/rust/blob/master/compiler/rustc_mir/src/transform/inline.rs</a>&gt;, AFAIK.</p>



<a name="237879817"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions%20for%20a%20beginner/near/237879817" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html#237879817">(May 07 2021 at 19:44)</a>:</h4>
<p><span class="user-mention silent" data-user-id="125270">scottmcm</span> <a href="#narrow/stream/189540-t-compiler.2Fwg-mir-opt/topic/Suggestions.20for.20a.20beginner/near/237750034">said</a>:</p>
<blockquote>
<p>Hmm, interesting -- with <code>-Z mir-opt-level=3</code> it'll inline <code>TryV2::from_output</code> and <code>TryV2::from_residual</code>, but not <code>TryV2::branch</code>: <a href="https://rust.godbolt.org/z/KTjE9hhxr">https://rust.godbolt.org/z/KTjE9hhxr</a></p>
<p>(And yes, <code>branch</code> is <code>#[inline]</code>: <a href="https://github.com/rust-lang/rust/blob/676ee14729462585b969bbc52f32c307403f4126/library/core/src/result.rs#L1660-L1666">https://github.com/rust-lang/rust/blob/676ee14729462585b969bbc52f32c307403f4126/library/core/src/result.rs#L1660-L1666</a> )</p>
</blockquote>
<p>Though it's not <code>#[inline(always)]</code> ;)</p>



<a name="237891192"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions%20for%20a%20beginner/near/237891192" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html#237891192">(May 07 2021 at 21:14)</a>:</h4>
<p>Sounds like a good PR to run perf against and see how it would impact things, after I get my <code>?</code> change in!</p>



<a name="238087325"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions%20for%20a%20beginner/near/238087325" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html#238087325">(May 10 2021 at 04:58)</a>:</h4>
<p>FYI, <span class="user-mention" data-user-id="409735">@Théo Degioanni</span>, I ended up making an issue for this so I can mention it in FIXMEs: <a href="https://github.com/rust-lang/rust/issues/85133">https://github.com/rust-lang/rust/issues/85133</a></p>
<p>Feel free to claim it if you'd still like to work on it!</p>



<a name="238093127"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions%20for%20a%20beginner/near/238093127" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Suggestions.20for.20a.20beginner.html#238093127">(May 10 2021 at 06:33)</a>:</h4>
<p>Thanks! Yes I'm still interested, but exams came early and I'll be busy until Wednesday. I should be able to look into that more after that.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>